<?php
declare(strict_types=1);
/**
 * 发送短信基类
 */
namespace Service\VenderClient\Sms;
use Service\Code\Normal;
use Service\BaseService;
use Service\ExtraService\TaskWorker;
use Service\ExtraService\Template;
class SmsChuanglanClient extends BaseService
{
    protected $config;
    public function __construct( array $apiBody = [] )
    {
        $this->config = $apiBody;
    }
    
    /**
     * 发送短信
     * @param string $mobile
     * @param string $message
     */
    public function send(string $mobile ,string $msg,string $temp = 'default')
    {
        $temp_contents = Template::$temp[$temp];
        $msg = str_replace('{code}',$msg,$temp_contents);
        $data= [
            'account'   => $this->config['username'],
            'password'  => $this->config['password'],
            'msg'       => "【{$this->config['signature']}】{$msg}",
            'phone'     => $mobile,
            'report'    => 'true'
        ];
        $url = "http://smssh1.253.com/msg/v1/send/json";
        $parameter = [
            [
                'tag'       => 'sms_chuanglan',
                'url'       => $url,
                'data'      => $data,
                'method'    => 'post',
                'format'    => 'json',
                'header'    => [],
            ],
        ];
        $resTask    = TaskWorker::instance()->addTask($parameter);
        $resTask    = current($resTask);
       
        $response   = json_decode($resTask['response']['result'],true);
        if( isset($response['code'])  && $response['code'] == '0' ) {
            return Normal::returnCode(Normal::SUC,"发送成功");
        }
        return Normal::returnCode(Normal::FAIL,'发送失败:'.json_encode($response,JSON_UNESCAPED_UNICODE));
    }
}
